home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / IO / sa / file < prev    next >
Text File  |  1996-08-10  |  12KB  |  390 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  3. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  4. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  5. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  6. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  7. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  8.  
  9. -------------------------------------------------------------------
  10. class FILE < $OSTREAM is
  11.    -- Buffered file.  Uses the C standard "FILE".  To avoid buffering
  12.    -- penalties, read or write entire files using an FSTR.  For systems
  13.    -- which do funny things for linefeeds and cntl-z, this is a text
  14.    -- file, not a binary finle, for which you should use BFILE.
  15.    --   f: FILE :=  FILE::open_for_read("test_file");
  16.    --   line: STR := f.get_line; -- gets the next line
  17.    --  
  18.    -- A more convenient way to parse files is to read them into a string
  19.    -- and then use a STR_CURSOR on them. This may even have significantly
  20.    -- better performance
  21.    --   f2: FILE := FILE::open_for_read("test_file");
  22.    --   whole_file: STR := f2.str;  -- The whole file as a string
  23.    --   cursor: STR_CURSOR := whole_file.cursor;
  24.    --   next_integer: INT := cursor.get_int; -- Get an integer from the file
  25.    --                     -- string
  26.    --   next_word: STR := cursor.get_word;    -- Read a word from the file
  27.    -- 
  28.  
  29.    readonly attr fp:EXT_OB;       -- The FILE pointer readonly attr
  30.  
  31.    stdin_macro:EXT_OB is builtin FILE_STDIN; end;
  32.    stdout_macro:EXT_OB is builtin FILE_STDOUT; end;
  33.    stderr_macro:EXT_OB is builtin FILE_STDERR; end;
  34.  
  35.    stdin:SAME is
  36.       -- A file object for stdin.
  37.       r::=new; r.fp:=stdin_macro; return r 
  38.    end;
  39.  
  40.    stdout:SAME is
  41.       -- A file object for stdout.
  42.       r::=new; r.fp:=stdout_macro; return r 
  43.    end;
  44.  
  45.    stderr:SAME is
  46.       -- A file object for stderr.
  47.       r::=new; 
  48.       r.fp:=stderr_macro; 
  49.       return r 
  50.    end;
  51.  
  52.    open_for_write(nm:STR):SAME is
  53.       -- Create a new file with name `nm' and default permissions.
  54.       -- File is truncated and opened for writing. 
  55.       r::=new; 
  56.       r.fp:=RUNTIME::fopen(nm,"w"); 
  57.       return r 
  58.    end;
  59.  
  60.    open_for_read(nm:STR):SAME is
  61.       -- A new object representing the file named `nm' accessible
  62.       -- for reading. 
  63.       r::=new; 
  64.       r.fp:=RUNTIME::fopen(nm,"r"); 
  65.       return r 
  66.    end;
  67.  
  68.    open_for_append(nm:STR):SAME is
  69.       -- A new object representing the file named `nm' accessible
  70.       -- for appending.  File is created if not existing.
  71.       r::=new; r.fp:=RUNTIME::fopen(nm,"a"); 
  72.       return r 
  73.    end;
  74.  
  75.    open_for_update_truncating(nm:STR):SAME is
  76.       -- A new object representing the file named `nm' accessible
  77.       -- for reading and writing.  File is truncated if existing.
  78.       r::=new; r.fp:=RUNTIME::fopen(nm,"w+"); 
  79.       return r 
  80.    end;
  81.  
  82.    open_for_update(nm:STR):SAME is
  83.       -- A new object representing the file named `nm' accessible
  84.       -- for reading and writing.
  85.       r::=new; r.fp:=RUNTIME::fopen(nm,"r+"); 
  86.       return r 
  87.    end;
  88.  
  89.    open_for_update_appending(nm:STR):SAME is
  90.       -- A new object representing the file named `nm' accessible
  91.       -- for reading and appending.  File is created if not existing.
  92.       r::=new; r.fp:=RUNTIME::fopen(nm,"a+"); return r 
  93.    end;
  94.  
  95.    eof:BOOL pre ~void(fp) is
  96.       -- true if EOF has been encountered.  Cleared by "clear".
  97.       return RUNTIME::feof(fp) 
  98.    end;
  99.  
  100.    error:BOOL is
  101.       -- true is an error has occurred with this file.  Cleared by "clear".
  102.       return void(fp) or RUNTIME::ferror(fp) 
  103.    end;
  104.    
  105.    clear pre ~void(fp) is
  106.       -- resets end_seen and error status.  Built-in.
  107.       builtin FILE_CLEAR;
  108.    end;
  109.    
  110.    size:INT pre ~void(fp) is
  111.       -- The size of self in characters. -1 for error.
  112.       current::=current_loc;
  113.       seek_from_end(0);
  114.       r::=current_loc;
  115.       seek_from_front(current);
  116.       return r 
  117.    end;
  118.  
  119.    seek_from_front(off:INT) pre ~void(fp) is
  120.       -- Attempt to move to character `off' in self.
  121.       dummy::=RUNTIME::fseek(fp,off,0) 
  122.    end;
  123.    
  124.    seek_from_current(off:INT) pre ~void(fp) is
  125.       -- Attempt to move `off' characters forward.
  126.       dummy::=RUNTIME::fseek(fp,off,1) 
  127.    end;
  128.  
  129.    seek_from_end(off:INT) pre ~void(fp) is
  130.       -- Attempt to move `off' characters back from the end of self.
  131.       dummy::=RUNTIME::fseek(fp,off,2) 
  132.    end;
  133.    
  134.    current_loc:INT pre ~void(fp) is
  135.       -- The current location in the file.
  136.       return RUNTIME::ftell(fp) 
  137.    end;
  138.  
  139.    str: STR pre ~void(fp) is return  fstr.str end;
  140.    
  141.    fstr:FSTR pre ~void(fp) is
  142.       -- A string buffer containing the contents of self.
  143.       --
  144.       -- Due to conversions on some systems, the number of
  145.       -- characters read may be less than the total number
  146.       -- of characters in the file.
  147.       fsize::=size;
  148.       if fsize = -1 then return void; end;
  149.       if fsize =  0 then
  150.      return #FSTR(1);
  151.       end;
  152.       r::=#FSTR(fsize);
  153.       seek_from_front(0);
  154.       bsize::=RUNTIME::fread(r,1,fsize,fp);
  155.       r.loc:=bsize;
  156.       return r 
  157.    end;
  158.  
  159.    fstr(start,msize:INT):FSTR pre ~void(fp) and start<size is
  160.       -- A string buffer of size at most `msize' containing successive
  161.       -- characters from self beginning at `start'.
  162.       --
  163.       -- The number of characters read may be less than 'msize'
  164.       -- if end-of-file was encountered or some characters have
  165.       -- been discarded due to system-specific conversions.
  166.       r::=#FSTR(msize);
  167.       seek_from_front(start);
  168.       bsize::=RUNTIME::fread(r,1,msize,fp);
  169.       r.loc:=bsize;
  170.       return r 
  171.    end;
  172.  
  173.    get_char:CHAR pre ~void(fp) is
  174.       return RUNTIME::fgetc(fp); 
  175.    end;
  176.  
  177.    get_str:STR is
  178.       -- A string containing the characters up to the next newline.
  179.       return get_line.str;
  180.    end;
  181.  
  182.    get_line:FSTR is
  183.       -- A string buffer containing the characters up to the next newline.
  184.       res::=#FSTR;
  185.       loop 
  186.           c::=get_char;
  187.       if eof then return res end;
  188.       res:=res.push(c);
  189.       until!(c='\n');
  190.       end;
  191.       return res;
  192.    end;
  193.  
  194.    get_up_to(sc:CHAR):STR is
  195.        -- A string buffer containing the characters up to the next c.
  196.        res::=#FSTR;
  197.        loop
  198.       c::=get_char;
  199.       if eof then return res.str end;
  200.       res:=res.push(c);
  201.       until!(c=sc);
  202.        end;
  203.        return res.str;
  204.     end;
  205.  
  206.    plus(s:$STR) is
  207.       -- Append the $STR "s" to the file
  208.       
  209.       if void(s) then return end;
  210.       -- This is a hack, to get around the void representation of "".
  211.  
  212.       -- Note that if this is ever called, we are assured
  213.       -- that s is _not_ one of CHAR, STR, FSTR, since these
  214.       -- cases are handled by the proper overloading. Hence,
  215.       -- this is the most general case and we call str.
  216.       -- BV (4/26/96)
  217.       ss::=s.str;
  218.       fs::=fwrite_str(ss,ss.length,fp)
  219.    end;
  220.    
  221.    plus(s:STR) is
  222.       -- Append the string "s" to the file
  223.       if void(s) then return end;
  224.       -- This is a hack, to get around the void representation of "".
  225.       fs::=fwrite_str(s,s.length,fp);
  226.    end;
  227.    
  228.    plus(s:FSTR) is
  229.       -- Append the string "s" to the file
  230.  
  231.       if void(s) then return end;
  232.       -- This is a hack, to get around the void representation of "".
  233.       fs::=fwrite_fstr(s,s.length,fp);
  234.    end;
  235.  
  236.    plus(c:CHAR) is
  237.       RUNTIME::fputc(c,fp)      
  238.    end;
  239.  
  240.    private fwrite_str(f:STR,sz:INT,fp:EXT_OB):INT is  -- Built in
  241.       builtin FILE_FWRITE_STR;
  242.    end;
  243.    
  244.    private fwrite_fstr(f:FSTR,sz:INT,fp:EXT_OB):INT is  -- Built in
  245.       builtin FILE_FWRITE_FSTR;
  246.    end;
  247.    
  248.    plus(s:$STR):SAME is
  249.       plus(s);
  250.       return self;
  251.    end;
  252.    
  253.    plus(s:STR):SAME is
  254.       plus(s);
  255.       return self;
  256.    end;   
  257.    
  258.    plus(s:FSTR):SAME is
  259.       plus(s);
  260.       return self;
  261.    end;      
  262.  
  263.    plus(c:CHAR):SAME is
  264.       RUNTIME::fputc(c,fp);
  265.       return self;
  266.    end;
  267.  
  268.    flush pre ~void(fp) is
  269.       -- Forces write of any buffered data.
  270.       RUNTIME::fflush(fp) 
  271.    end;
  272.    
  273.    buffer_size(sz:INT) pre ~void(fp) is
  274.       -- Set buffer size to `sz' bytes.  Must be called after
  275.       -- a file is opened but before reading or writing.
  276.       RUNTIME::setbuffer(fp,#FSTR(sz),sz) 
  277.    end;
  278.  
  279.    close pre ~void(fp) is
  280.       -- Close the file corresponding to self.
  281.       RUNTIME::fclose(fp) 
  282.    end;
  283.    
  284.    delete(nm:STR) is
  285.       -- Delete a file.
  286.       RUNTIME::unlink(nm) end;
  287.  
  288.    temp_file:SAME is
  289.       -- Open a temporary file for writing.  It will be deleted
  290.       -- automatically when the process terminates.
  291.       r::=new; r.fp:=RUNTIME::tmpfile; return r 
  292.    end;
  293.  
  294.    temp_filename(prefix:STR):STR is
  295.       -- Generate a unique name somewhere appropriate.
  296.       e::=RUNTIME::tempnam(void,prefix);
  297.       if void(e) then raise "Couldn't get temporary filename"; end;
  298.       return STR::create_from_c_string(e);
  299.    end;
  300.  
  301.    resolve_path(fn:STR):STR is
  302.       -- Path with ".", "..", and any symbolic links resolved.
  303.       -- NOT IMPLEMENTED CORRECTLY.
  304.       return fn;
  305.    end;
  306.  
  307.    current_directory:STR is
  308.       -- Absolute path of the current directory.
  309.       buf::=#FSTR(1024); -- Buffer to hold C string
  310.       e::=RUNTIME::getwd(buf);
  311.       if void(e) then raise "Couldn't get current directory path"; end;
  312.       return STR::create_from_c_string(e);
  313.    end;
  314.  
  315.    create_directory(nm:STR) is
  316.       -- Make a directory with the path `nm'.
  317.       RUNTIME::mkdir(nm,0o777);
  318.    end;
  319.  
  320. end; -- class FILE
  321. -------------------------------------------------------------------------
  322. class BFILE is
  323.    -- A binary file.  For many systems this should behave the
  324.    -- same as FILE.  This class is untested and not used in the
  325.    -- compiler.
  326.  
  327.    include FILE;
  328.  
  329.    open_for_write(nm:STR):SAME is
  330.       -- Create a new file with name `nm' and default permissions.
  331.       -- File is truncated and opened for writing.
  332.       r::=new; r.fp:=RUNTIME::fopen(nm,"wb"); return r 
  333.    end;
  334.  
  335.    open_for_read(nm:STR):SAME is
  336.       -- A new object representing the file named `nm' accessible
  337.       -- for reading.
  338.       r::=new; r.fp:=RUNTIME::fopen(nm,"rb"); return r 
  339.    end;
  340.  
  341.    open_for_append(nm:STR):SAME is
  342.       -- A new object representing the file named `nm' accessible
  343.       -- for appending.  File is created if not existing.
  344.       r::=new; r.fp:=RUNTIME::fopen(nm,"ab"); return r 
  345.    end;
  346.  
  347.    open_for_update_truncating(nm:STR):SAME is
  348.       -- A new object representing the file named `nm' accessible
  349.       -- for reading and writing.  File is truncated if existing.
  350.       r::=new; r.fp:=RUNTIME::fopen(nm,"w+b"); return r 
  351.    end;
  352.  
  353.    open_for_update(nm:STR):SAME is
  354.       -- Returns a  new object representing the file named `nm' accessible
  355.       -- for reading and writing.
  356.       r::=new; r.fp:=RUNTIME::fopen(nm,"r+b"); return r 
  357.    end;
  358.  
  359.    open_for_update_appending(nm:STR):SAME is
  360.       -- A new object representing the file named `nm' accessible
  361.       -- for reading and appending.  File is created if not existing.
  362.       r::=new; r.fp:=RUNTIME::fopen(nm,"a+b"); return r 
  363.    end;
  364.  
  365.    fstr:FSTR pre ~void(fp) is
  366.       -- Returns a string buffer containing the contents of self.
  367.       sz::=size; 
  368.       if sz= -1 then return void; end;
  369.       r::=#FSTR(sz);
  370.       seek_from_front(0);
  371.       fs::=RUNTIME::fread(r,sz,1,fp);
  372.       r.loc:=sz;
  373.       return r 
  374.    end;
  375.    
  376.    fstr(st,sz:INT):FSTR pre ~void(fp) is
  377.       -- A string buffer of size at most `sz' containing successive 
  378.       -- characters from self beginning at `st'.
  379.       fsz::=size;
  380.       asz::=(fsz-st).min(sz);
  381.       r::=#FSTR(asz);
  382.       seek_from_front(0);
  383.       fs::=RUNTIME::fread(r,asz,1,fp);
  384.       r.loc:=asz;
  385.       return r 
  386.    end;
  387.  
  388. end;
  389. -----------------------------------------------------------------------------
  390.